#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
【需求背景】
目前通过店铺分支加工的KP联系方式量级比较少，需要检查这个链路生产是否有问题
'''

import sys

sys.path.append('/mnt/moonbrook/Moonbrook')
from aes import contact_aes, name_aes
import os
import re
import json
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig, RobotConfig
from utils import Utils
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    week_min, week_max = Utils.get_date_range(ndays=7)


    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col

    def is_has_mobile(self,b2bContact):
        for dic in b2bContact:
            if dic.get('contactType') == 0:
                return True


    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        # 解密，提取字段数据
        record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True,write_error_f=self.write_error_f)
        # ["contact", "nameId", "name", "socialCreditCode", "regNumber", "contactType", "exportTime"]
        b2bContact = record_dic.get('b2bContact', [])
        b2bSource = record_dic.get('b2bSource', '')
        nameId = record_dic.get('nameId', '')
        b2bEntityType = record_dic.get('b2bEntityType', '') # str
        b2bLinkAccessible = record_dic.get('b2bLinkAccessible', '') # bool
        b2bPageUrl = record_dic.get('b2bPageUrl', '')
        b2bEnterpriseName = record_dic.get('b2bEnterpriseName', '')
        b2bServiceYear = record_dic.get('b2bServiceYear', '') #int

        # 验证数据的合法性
        must_exists_values = [b2bSource]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        if not self.is_data_legal(must_exists_values, must_has_one_exists_values):
            return save_result_dic

        dc_ls = ['b2bPageUrl', 'b2bContact', 'b2bEnterpriseName']
        dc_dict = {}
        for dc_field_name in dc_ls:
            dc_field_value = record_dic.get(dc_field_name,'')
            dc_dict[dc_field_name] = dc_field_value
        # 真正业务逻辑
        if b2bSource in ["hc360"]:
            if isinstance(b2bServiceYear,(int,float)):
                save_result_dic['hc360_b2bServiceYear_count'] += 1
                if self.is_has_mobile(b2bContact):
                    save_result_dic['hc360_b2bServiceYear_has_mobile_count'] += 1
                    self.write_dc_f.write(
                        '{}\n'.format(json.dumps(dc_dict, ensure_ascii=False)))
                    if nameId:
                        save_result_dic['hc360_b2bServiceYear_has_mobile_and_nameId_count'] += 1
                        save_result_dic['hc360_b2bServiceYear_has_mobile_and_nameId_dup'].add(nameId)


        elif b2bSource in ["b2b_baidu"]:
            if isinstance(b2bServiceYear, (int, float)):
                save_result_dic['b2b_baidu_b2bServiceYear_count'] += 1
                if self.is_has_mobile(b2bContact):
                    save_result_dic['b2b_baidu_b2bServiceYear_has_mobile_count'] += 1
                    self.write_dc_f.write(
                        '{}\n'.format(json.dumps(dc_dict, ensure_ascii=False)))
                    if nameId:
                        save_result_dic['b2b_baidu_b2bServiceYear_has_mobile_and_nameId_count'] += 1
                        save_result_dic['b2b_baidu_b2bServiceYear_has_mobile_and_nameId_dup'].add(nameId)

        elif b2bSource in ["1688"]:
            if b2bEntityType:
                save_result_dic['1688_b2bEntityType_count'] += 1
                if self.is_has_mobile(b2bContact):
                    save_result_dic['1688_b2bEntityType_has_mobile_count'] += 1
                    self.write_dc_f.write(
                        '{}\n'.format(json.dumps(dc_dict, ensure_ascii=False)))
                    if nameId:
                        save_result_dic['1688_b2bEntityType_has_mobile_and_nameId_count'] += 1
                        save_result_dic['1688_b2bEntityType_has_mobile_and_nameId_dup'].add(nameId)
            if b2bLinkAccessible:
                save_result_dic['1688_b2bLinkAccessible_count'] += 1
                if self.is_has_mobile(b2bContact):
                    save_result_dic['1688_b2bLinkAccessible_has_mobile_count'] += 1
                    self.write_dc_f.write(
                        '{}\n'.format(json.dumps(dc_dict, ensure_ascii=False)))
                    if nameId:
                        save_result_dic['1688_b2bLinkAccessible_has_mobile_and_nameId_count'] += 1
                        save_result_dic['1688_b2bLinkAccessible_has_mobile_and_nameId_dup'].add(nameId)






        return save_result_dic

    def run(self):
        # 需要统计的维度
        save_result_dic = {
            'hc360_b2bServiceYear_count':0,  # 中b2bSource是hc360且b2bServiceYear有值的店铺量级
            'hc360_b2bServiceYear_dup':Utils.bloom_filter_fac(),
            'hc360_b2bServiceYear_has_mobile_count': 0, # 店铺中b2bContact有值、且contactType为0（mobile）的店铺量级
            'hc360_b2bServiceYear_has_mobile_dup': Utils.bloom_filter_fac(),
            'hc360_b2bServiceYear_has_mobile_and_nameId_count': 0, # 店铺中b2bContact有值、且contactType为0（mobile）且有nameId的店铺量级
            'hc360_b2bServiceYear_has_mobile_and_nameId_dup': Utils.bloom_filter_fac(),

            'b2b_baidu_b2bServiceYear_count': 0,
            'b2b_baidu_b2bServiceYear_dup': Utils.bloom_filter_fac(),
            'b2b_baidu_b2bServiceYear_has_mobile_count': 0,
            'b2b_baidu_b2bServiceYear_has_mobile_dup': Utils.bloom_filter_fac(),
            'b2b_baidu_b2bServiceYear_has_mobile_and_nameId_count': 0,
            'b2b_baidu_b2bServiceYear_has_mobile_and_nameId_dup': Utils.bloom_filter_fac(),

            '1688_b2bEntityType_count': 0,
            '1688_b2bEntityType_dup': Utils.bloom_filter_fac(),
            '1688_b2bEntityType_has_mobile_count': 0,
            '1688_b2bEntityType_has_mobile_dup': Utils.bloom_filter_fac(),
             '1688_b2bEntityType_has_mobile_and_nameId_count': 0,
            '1688_b2bEntityType_has_mobile_and_nameId_dup': Utils.bloom_filter_fac(),

            '1688_b2bLinkAccessible_count': 0,
            '1688_b2bLinkAccessible_dup': Utils.bloom_filter_fac(),
            '1688_b2bLinkAccessible_has_mobile_count': 0,
            '1688_b2bLinkAccessible_has_mobile_dup': Utils.bloom_filter_fac(),
            '1688_b2bLinkAccessible_has_mobile_and_nameId_count': 0,
            '1688_b2bLinkAccessible_has_mobile_and_nameId_dup': Utils.bloom_filter_fac(),

        }
        # 查询mongo的配置，这些配置写在一起主要是为了借鉴起来方便
        config = MongoQueryConfig.dsf_s33_tj_config()
        result_dic = self.get_data_from_mongo(config, save_result_dic)


def run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '店铺量级統計'
    run(file_name)
    # scheduler_run(file_name)
